$(document).ready(function () {
    $("#file_upload").uploadify({
        'auto'     : false,
        'swf'      : '/static/exec/upload/uploadify.swf',
        'uploader' : '/static/exec/upload/uploadify.php',
        'onUploadComplete' : function(file) {
            $("#logo").val(file.name);
        }
    });
    $("#addSaleCat").click(function () {
        $.ajax({
            url:'/admin/salecategory/create',
            dataType:'json',
            type:"POST",
            data:{ catName:$("#catName").val(), catOrder:$("#catOrder").val() },

            success:function (data) {

                alert(data);
                $("#myModal2").modal('hide');
                window.location.reload();
            }

        });
        return false;
    });
    $('#formCreateCustomer').on('submit', function () {

        $.ajax({
            url:'/admin/customer/create',
            dataType:'json',
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {
                if (data > 0)
                    alert('Successful!');
                else
                    alert('Fail');
                $("#myModal2").modal('hide');
                window.location.reload();
            }
        });
        return false;
    });

    $('#formCreateRealCat').on('submit', function () {

        $.ajax({
            url:'/admin/realcategory/create',
            dataType:'json',
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {
                if (data > 0)
                    alert('Successful!');
                else
                    alert('Fail');
                $("#myModal2").modal('hide');
                window.location.reload();
            }
        });
        return false;
    });


    $('#formUpdate').on('submit', function () {

        $.ajax({
            url:'/admin/salecategory/update',
            dataType:'json',
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {

                if (data > 0)
                    alert('Successful!');
                else
                    alert('Fail');
                $("#myModal3").modal('hide');
                window.location.reload();
            }

        });

        return false;
    });

    $('#formUpdateReal').on('submit', function () {

        $.ajax({
            url:'/admin/realcategory/update',
            dataType:'json',
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {

                if (data > 0)
                    alert('Successful!');
                else
                    alert('Fail');
                $("#myModal3").modal('hide');
                window.location.reload();
            }

        });

        return false;
    });

    $('#saleCategory a').on('click', function () {

        var $this = $(this);

        if ($this.hasClass('update')) {
            var $modal = $($this.attr('href')),
                params = $this.data('params');
            $modal.find('#ucatName').val(params.SaleCategoryName);
            $modal.find('#ucatOrder').val(params.catOrder);
            $modal.find('#catId').val(params.SaleCategoryID);
            $modal.modal('show');
        }
        else if ($this.hasClass('delete')) {
            if (confirm('Bạn có chắc chắn muốn xóa?')) {
                var $this = $(this);

                $.ajax({
                    url:'/admin/salecategory/delete/id/' + parseInt($this.parents('tr').attr('id')),
                    dataType:'json',
                    type:"POST",

                    success:function (data) {
                        if (data > 0)
                            alert('Successful');
                        else
                            alert('Fail');

                        window.location.reload();
                    }
                });
            }
        }
    });
    $('#customer a').on('click', function () {

        var $this = $(this);

        if ($this.hasClass('update')) {
            var $modal = $($this.attr('href')),
                params = $this.data('params');
            $modal.find('#CustomerName').val(params.CustomerName);
            $modal.find('#Address').val(params.Address);
            $modal.find('#Tel').val(params.Tel);
            $modal.find('#Mobile').val(params.Mobile);
            $modal.find('#Email').val(params.Email);
            $modal.find('#Fax').val(params.Fax);
            $modal.find('#CustomerType').val(params.CustometType);
            $modal.find('#Logo').val(params.Logo);
            $modal.find('#Contact').val(params.Contact);
            $modal.find('#Website').val(params.Website);
            $modal.find('#customerid').val(params.CustomerID);
            $modal.modal('show');
        }
        else if ($this.hasClass('delete')) {
            if (confirm('Bạn có chắc chắn muốn xóa?')) {
                var $this = $(this);

                $.ajax({
                    url:'/admin/customer/delete/id/' + parseInt($this.parents('tr').attr('id')),
                    dataType:'json',
                    type:"POST",

                    success:function (data) {
                        if (data > 0)
                            alert('Successful');
                        else
                            alert('Fail');

                        window.location.reload();
                    }
                });
            }
        }
    });
    $('#realCategory a').on('click', function () {

        var $this = $(this);

        if ($this.hasClass('update')) {
            var $modal = $($this.attr('href')),
                params = $this.data('params');
            $modal.find('#ucatName').val(params.RealCategoryName);
            $modal.find('#ucatOrder').val(params.catOrder);
            $modal.find('#ucatDesc').val(params.RealCategoryDesc);
            $modal.find('#catId').val(params.RealCategoryID);
            $modal.modal('show');
        }
        else if ($this.hasClass('delete')) {
            if (confirm('Bạn có chắc chắn muốn xóa?')) {
                var $this = $(this);

                $.ajax({
                    url:'/admin/realcategory/delete/id/' + parseInt($this.parents('tr').attr('id')),
                    dataType:'json',
                    type:"POST",

                    success:function (data) {
                        if (data > 0)
                            alert('Successful');
                        else
                            alert('Fail');

                        window.location.reload();
                    }
                });
            }
        }
    });
})